package com.example.expresscabinet.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.expresscabinet.common.Result;
import com.example.expresscabinet.dao.userMapper;
import com.example.expresscabinet.model.user;
import com.example.expresscabinet.service.IUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class userServiceImpl extends ServiceImpl<userMapper, user> implements IUserService {
    @Resource
    userMapper userMapper ;

    public Result<?> existUser(user user){
        QueryWrapper<user> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tel", user.getTel());
        queryWrapper.eq("password", user.getPassword());
        queryWrapper.eq("role", user.getRole());
        user res = userMapper.selectOne(queryWrapper);
        if (res == null) {
            return Result.error("-1", "手机号或密码错误");
        }
        return Result.success(res);
    }

    public Result<?> addUser(user user){
        QueryWrapper<user> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tel", user.getTel());
        queryWrapper.eq("password", user.getPassword());
        queryWrapper.eq("role", user.getRole());
        user res = userMapper.selectOne(queryWrapper);
        if (res != null) {
            return Result.error("-1", "用户名已注册");
        }
        userMapper.insert(user);
        return Result.success();
    }


    @Override
    public Result<?> deleteExpressman(String tel){
        QueryWrapper<user> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tel", tel);
        queryWrapper.eq("task", 0);
        user res = userMapper.selectOne(queryWrapper);
        if (res == null) {
            return Result.error("-1", "该快递员还存在取件任务不可删除");
        }
        userMapper.deleteById(tel);
        return Result.success();
    }


    //查询合适的快递员
    @Override
    public String queryFixExpressman(String company){
        String res=null;
        List<user> expressmanList= userMapper.getFixExpressman(company);
        if(expressmanList!=null)
        {
            res= expressmanList.get(0).getTel();
            System.out.println(res);
        }
        return res;
    }


    //查询合适的快递员
    @Override
    public Page<user> queryExpressman(int pageNum,int pageSize){
        QueryWrapper<user> wrapper=new QueryWrapper<>();
        wrapper.eq("role",2);
        Page<user> expressmanList= userMapper.selectPage(new Page<>(pageNum,pageSize),wrapper);
        return expressmanList;
    }


    @Override
    public int[] queryList(){
        int[]list=new int[5];
        String []companies={"顺丰","中通","申通","圆通","韵达"};
        for(int i=0;i<5;i++){
            QueryWrapper<user> wrapper=new QueryWrapper<>();
            wrapper.eq("company",companies[i]);
            List<user> expressmanList= userMapper.selectList(wrapper);
            int sum=0;
            for(user exp:expressmanList){
                sum+=exp.getTask();
            }
            list[i]=sum;
        }
        return list;
    }
}
